package com.kornero.auto.controller;

import com.kornero.auto.view.groups.IMainGroup;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainGroupController extends AbstractController<IMainGroup> {

    private static final Logger logger = LoggerFactory.getLogger(MainGroupController.class);

    private final ExecutorService threadPool = Executors.newSingleThreadExecutor();

    private final Runnable task;

    public MainGroupController(Runnable task, IMainGroup mainGroup) {
        super(mainGroup);
        this.task = task;
        this.bind();
    }

    private void bind() {
        this.getView().addRequestListener(new Listener() {
            @Override
            public void handleEvent(Event event) {
                doRequest();
            }
        });
    }

    private void doRequest() {
        threadPool.submit(task);
    }

    @Override
    public void stop() {
        threadPool.shutdownNow();
    }
}
